import { STComponent } from '@delon/abc/st';
/*
 * @Description  : 
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2021-12-29 14:51:07
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-04-28 22:12:14
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\order-management\\modal\\vehicle\\modify-rate\\modify-rate.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */

import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { STColumn } from '@delon/abc/st';
import { SFComponent, SFNumberWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { OrderManagementService } from '../../../services/order-management.service';

@Component({
  selector: 'app-order-management-vehicle-modify-rate',
  templateUrl: './modify-rate.component.html',
  styleUrls: ['./modify-rate.component.less']
})
export class VehicleModifyRateComponent implements OnInit {
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('st', { static: false }) st!: STComponent;
  schema: SFSchema = {};
  ui: SFUISchema = {};
  columns: STColumn[] = [
    { title: '订单号', index: 'billCode' },
    { title: '原附加费率', render: 'oldAdditionalRate' },
    { title: '原附加费', render: 'oldSurcharge' },
    { title: '新附加费', render: 'newSurcharge' },
  ];
  aggreechecked = false;
  modiUrl: string= this.service.$api_searchAdditionalRateBatch
  @Input()
  data: any;
  dataList: any;

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public service: OrderManagementService) {}

  ngOnInit(): void {
    console.log(this.data);
    this.initSF();
    this.initData();
    console.log(this.data?.ids)
  }
  get reqParams() {
    return { 
      // operateObject: this.i?.resourceCode,
      // operateType: 4,
      ids: this.data?.ids,
      additionalRate: this.sf?.value?.additionalRate || 0
     };
  }
  initData() {
    console.log(this.reqParams);
    
    this.service.request(this.service.$api_searchAdditionalRateBatch, this.reqParams).subscribe((res: any) => {
      console.log(res);
      this.dataList = res
    })
  }
  initSF() {
    this.schema = {
      properties: {
        additionalRate: {
          type: 'number',
          title: '附加费率',
          maximum: 99,
          minimum: 0,
          ui: {
            unit: '%',
            widgetWidth: 200,
            precision: 2,
            change:(item: any) => {
              console.log(item);
              setTimeout(() => {
              this.initData() 
              });
            }
          } as SFNumberWidgetSchema
        }
      },
      required: ['additionalRate']
    };
    this.ui = {
      '*': {
        spanLabelFixed: 100,
        grid: { span: 16 }
      }
    };
  }

  save(value: any): void {
    if (!this.sf?.value?.additionalRate) {
      this.service.msgSrv.warning('请填写附加费！');
      return;
    }
    console.log(this.sf?.value);
    const params = {
      ids: this.data?.ids,
      ...this.sf?.value
    };
    console.log(params);
    this.service.request(this.service.$api_set_updateAdditionalRateBatch, params).subscribe((res: any) => {
      if (res) {
        this.modal.close(true);
        this.service.msgSrv.success('变更运费成功');
      } else {
        this.service.msgSrv.error(res?.msg);
      }
    });
  }

  close(): void {
    this.modal.destroy();
  }
}
